#include "punctuationsview.h"

#include <QColorDialog>
#include <QFontDialog>
#include <QGridLayout>
#include <QFile>
#include <QHBoxLayout>

PunctuationsView::PunctuationsView(SoftKeyboard *parent) : QWidget(parent)

{
    this->parent = parent;

    punctuations->dataStrings = loadSymbols(":/data/symbol.txt");
    punctuations->setUnitMinHeight(40);
    punctuations->setUnitMinWidth(50);
    VDragableWidget = new MyDragableWidget(punctuations,MyDragableWidget::VERTICAL1);
    VDragableWidget->setMouseSensitivity(5);


    setRightToolWidget();
    //initKeyboard();
    QHBoxLayout *hLayout = new QHBoxLayout(this);
    hLayout->addWidget(punctuations, 5);
    QWidget *w = new QWidget;
    w->setLayout(layout);
    hLayout->addWidget(w, 1);


    connect(punctuations, &MyVDragableTranslateView::clicked, this, &PunctuationsView::userSelectPunctuation);
}
void PunctuationsView::userSelectPunctuation(const QString &text, int index)
{
    parent->addCandidateCharacterText(text);
}
/**
 * @brief PunctuationsView::setRightToolWidget
 * 设置右工具栏
 */
void PunctuationsView::setRightToolWidget()
{
    layout = new QVBoxLayout;
    //删除按钮
    btnDel = new MyPushButton("删除", Qt::Key_Backspace, this);
    connect(btnDel, &MyPushButton::clicked1, parent, &SoftKeyboard::deleteSlot);

    //确认按钮
    btnSure = new MyPushButton("无",0,this);

    //abc按钮
    btnabc = new MyPushButton("无",0,this);

    //123按钮
    btn123 = new MyPushButton("无",0,this);

    //上一页
    btnLast = new MyPushButton("无",0,this);


    //下一页
    btnBack = new MyPushButton("返回",Qt::Key_Back, this);

//    设置按钮在布局中大小变化的属性，设置成随着布局的变化变化
    btnDel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    btnSure->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    btnabc->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    btn123->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    btnLast->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    btnBack->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);

    //设置右侧工具栏布局
    layout->addWidget(btnDel,1);
    layout->addWidget(btnSure,1);
    layout->addWidget(btnabc,1);
    layout->addWidget(btn123,1);
    layout->addWidget(btnLast,1);
    layout->addWidget(btnBack,1);
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(1);

    /*将布局设置给右侧工具栏部件*/
//    ui->rightTool->setLayout(layout);
}

QStringList PunctuationsView::loadSymbols(const QString &file)
{
    QFile symbol_file(file);
    QStringList symbols;
    if (symbol_file.open(QIODevice::ReadOnly))
    {
        QString lineSymbols;
        while (!symbol_file.atEnd())
        {
            lineSymbols = QString::fromUtf8(symbol_file.readLine());
            lineSymbols = lineSymbols.trimmed();
            for (int i = 0; i < lineSymbols.size(); ++i)
            {
                symbols.append(lineSymbols.at(i));
            }
        }
        symbol_file.close();
    }

    return symbols;
}

/**
 * @brief PunctuationsView::initPunctuation
 * 初始化符号按钮
 */
void PunctuationsView::initPunctuation()
{

}




/**
 * @brief PunctuationsView::initKeyboard
 * 初始化键盘布局
 */
void PunctuationsView::initKeyboard()
{
    gridLayout = new QGridLayout;
    gridLayout->setSpacing(0);

    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 4; j++) {
            gridLayout->addWidget(keyBtns[4*i+j], i, j,1,1);
        }
    }
}

/**
 * @brief PunctuationsView::changePage
 * 符号换页，当前只有两页
 */
void PunctuationsView::changePage()
{
    if(page == 1){
        this->page++;
    }else{
        this->page--;
    }
    initPunctuation();
    initKeyboard();
}

/**
 * @brief PunctuationsView::renderButton
 * @param value
 * @return
 * 统一设置控件样式
 */
QToolButton* PunctuationsView::renderButton(QString value)
{
    QFont *font = new QFont("微软雅黑",10,1);
    QToolButton* toolBtn = new QToolButton();
    toolBtn->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    //设置按钮属性
//    toolBtn->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    toolBtn->setFont(*font);
    if(value == '&'){
        value = "&&";
    }
    toolBtn->setText(value);
    return toolBtn;
}

/**
 * @brief PunctuationsView::getValue
 * @param value
 * 获取按钮的值并传递到父布局中
 */
void PunctuationsView::getValue(QString value)
{
    parent->addCandidateCharacterText(value);
}

PunctuationsView::~PunctuationsView()
{
//    delete ui;
}
